//
//  NetworkTools.swift
//  SinaWeibo
//
//  Created by 张洪毓 on 15/11/17.
//  Copyright © 2015年 张洪毓. All rights reserved.
//

import UIKit
import AFNetworking

//
private let weiboDomain = "com.baidu.data.error"
//线上
private let hostName = "https://api.weibo.com/"
//测试
//private let hostName = "https://test.weibo.com/"
//定义请求方法的枚举
enum HTTPMethod: String {
    case POST = "POST"
    case GET = "GET"
}

class NetworkTools: AFHTTPSessionManager {

    //定义单例对象
    static var sharedTools: NetworkTools = {
//        let urlString = "https://api.weibo.com/"
        let url = NSURL(string: hostName)
        let tools = NetworkTools(baseURL: url)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    //加载字典数据
    func requestJsonDict(method: HTTPMethod,urlString: String,parameters: [String: AnyObject]?,finished: (result: [String: AnyObject]?,error: NSError?) -> ()) {
        if method == HTTPMethod.GET {
            GET(urlString, parameters: parameters, success: { (_, result) -> Void in
                //校验数据是否是字典
                if let dict = result as? [String: AnyObject] {
                    //执行成功的回调
                    finished(result: dict, error: nil)
                    return
                }
                //自定义错误信息，一般为数据格式不匹配
                let dataError = NSError(domain: weiboDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey: "数据错误"])
                finished(result: nil, error: dataError)
                
                }, failure: { (_, error) -> Void in
                    //执行错误的回调
                    finished(result: nil, error: error)
            })
        }else {
            POST(urlString, parameters: parameters, success: { (_, result) -> Void in
                if let dict = result as? [String: AnyObject] {
                    //执行成功的回调
                    finished(result: dict, error: nil)
                    return
                }
                //自定义错误信息，一般为数据格式不匹配
                let dataError = NSError(domain: weiboDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey: "数据错误"])
                finished(result: nil, error: dataError)
                
                }, failure: { (_, error) -> Void in
                    //执行错误的回调
                    finished(result: nil, error: error)
            })
        }
    }
    
    //添加上传图片的方法
    
    func uploadImage(urlString: String,parameters: [String: AnyObject]?,imageData: NSData,finished: (result: [String: AnyObject]?,error: NSError?) -> ()) {
       
        POST(urlString, parameters: parameters, constructingBodyWithBlock: { (multipartFormData) -> Void in
            multipartFormData.appendPartWithFileData(imageData, name: "pic", fileName: "OMG", mimeType: "image/jpeg")
            }, success: { (_, result) -> Void in
                if let dict = result as? [String: AnyObject] {
                    finished(result: dict, error: nil)
                }
            }) { (_, error) -> Void in
                finished(result: nil, error: error)
        }
    
    
    }

}
